package com.itheima.bos.service.impl;

import com.itheima.bos.dao.IRoleDao;
import com.itheima.bos.domain.Function;
import com.itheima.bos.domain.Role;
import com.itheima.bos.service.IRoleService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Mr.King on 2017/3/16.
 */
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    @Autowired
    private ProcessEngine processEngine;


    //添加角色时，将数据同步到activiti中
    @Override
    public void save(Role model,String functionIds) {
        roleDao.save(model);//持久态对象

        //创建保存组
        Group group = new GroupEntity();
        group.setId(model.getName());
        processEngine.getIdentityService().saveGroup(group);

        String[] split = functionIds.split(",");
        for (String s : split){
            //角色关联权限对象
            Function function = new Function();
            function.setId(s);
            model.getFunctions().add(function);
        }
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
}